<?php

namespace App\Http\Controllers\Batch;

use Illuminate\Http\Request;

use Session;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Service\Batch\BatchService;

class BatchController extends Controller
{

    /**
     * @param Request $request
     * 账号批量续费
     */
    public function BatchRenewAccount(Request $request)
    {

        $where = [];
        $where = $request->except('_token');
        $where['token'] = Session::get('MEMBER_TOKEN');

        $batch = new BatchService();
        $data = $batch->BatchRenewAccount($where);
        if($data['error_code'] == '0')
        {
            echo '<script>location.href="/account";alert("账号续费改成功！");</script>';
        }else{
            echo "<script>confirm('{$data['error_message']}')?location.href='/account' : history.back() ;</script>";
            exit();
        }

    }



    public function BatchAddAccount(Request $request)
    {
        $where = [];
        $where = $request->except('_token');
        $where['token'] = Session::get('MEMBER_TOKEN');

        if (preg_match("/[\x7f-\xff]/", $where['batch-add-username']))
        {
            echo "<script>confirm('非法的用户名'); history.back() ;</script>";
            exit();
        }

        if (preg_match("/[\x7f-\xff]/", $where['batch-password']))
        {
            echo "<script>confirm('非法的密码'); history.back() ;</script>";
            exit();
        }



        preg_match_all('/\d+/',$where['batch-add-username'],$arr);

        $num = 0;

        if(!empty($arr[0]))
        {
            $num = $arr[0];

            $where['batch-add-username'] = strstr($where['batch-add-username'],"$num[0]",true);
        }
        $name = [];

        for($i=1;$i<$where['batch-add-num']+1;$i++)
        {
            if($num[0])
            {

                $number =  $i+$num[0];
                $name[] .= $where['batch-add-username'].$number;

            }else{
                $name[] .= $where['batch-add-username'].$i;
            }
        }

        $where['batch-add-username'] = implode($name,',');


        $batch = new BatchService();
        $data = $batch->BatchAddAccount($where);

        if($data['error_code'] == '0')
        {
            echo '<script>location.href="/account";alert("批量添加成功！");</script>';
        }else{
            echo "<script>confirm('{$data['error_message']}')?location.href='/account' : history.back() ;</script>";
            exit();
        }

    }
//    public function BatchAddAccount(Request $request)
//    {
//        $where = [];
//        $where = $request->except('_token');
//        $where['token'] = Session::get('MEMBER_TOKEN');
//
//        if (preg_match("/[\x7f-\xff]/", $where['batch-add-username']))
//        {
//            echo "<script>confirm('非法的用户名'); history.back() ;</script>";
//            exit();
//        }
//
//        if (preg_match("/[\x7f-\xff]/", $where['batch-password']))
//        {
//            echo "<script>confirm('非法的密码'); history.back() ;</script>";
//            exit();
//        }
//
//        $name = [];
//        for($i=1;$i<$where['batch-add-num']+1;$i++)
//        {
//            $name[] .= $where['batch-add-username'].$i;
//        }
//        $where['batch-add-username'] = implode($name,',');
//
//        $batch = new BatchService();
//        $data = $batch->BatchAddAccount($where);
//
//        if($data['error_code'] == '0')
//        {
//            echo '<script>location.href="/account";alert("批量添加成功！");</script>';
//        }else{
//            echo "<script>confirm('{$data['error_message']}')?location.href='/account' : history.back() ;</script>";
//            exit();
//        }
//
//    }
    


}
